import Vue from 'vue'
export const noop = () => {}

const getScroll = (w, top) => {
  let ret = top ? w.pageYOffset : w.pageXOffset
  const method = top ? 'scrollTop' : 'scrollLeft'
  if (typeof ret !== 'number') {
    const d = w.document
    // ie6,7,8 standard mode
    ret = d.documentElement[method]
    if (typeof ret !== 'number') {
      // quirks mode
      ret = d.body[method]
    }
  }
  return ret
}

const getClientPosition = elem => {
  let box = null
  let x
  let y
  const doc = elem.ownerDocument
  const body = doc.body
  const docElem = doc && doc.documentElement
  box = elem.getBoundingClientRect()
  x = box.left
  y = box.top
  x -= docElem.clientLeft || body.clientLeft || 0
  y -= docElem.clientTop || body.clientTop || 0
  return {
    left: x,
    top: y
  }
}

export const getOffsetLeft = el => {
  const pos = getClientPosition(el)
  const doc = el.ownerDocument
  const w = doc.defaultView || doc.parentWindow
  pos.left += getScroll(w)
  return pos.left
}
/**
 *  获取对象深层级的对象，避免循环
 * @export
 * @param {*} sourceObj 源对象 {a:{b:{d:1}}}
 * @param {*} pathName  path组成的数组 例如：'a.b.c'
 * @param {*} defaultValue 如果没有值默认的值 []
 */
export function getValueByPath(sourceObj, pathName, defaultValue) {
  const getValue = function(sourceObj, pathNameArray) {
    const key = pathNameArray.shift()
    const value = sourceObj[key]
    if (value === undefined || value === null) {
      return defaultValue
    } else if (pathNameArray.length === 0) {
      return value
    } else if (pathNameArray.length > 0) {
      return getValue(value, pathNameArray)
    }
  }
  const pathNameArray = pathName.split('.')
  return getValue(sourceObj, pathNameArray)
}
/**
 *
 * 判断两个数是否相等
 * @param {*} a
 * @param {*} b
 * @returns
 */
export const valueEquals = (a, b) => {
  // see: https://stackoverflow.com/questions/3115982/how-to-check-if-two-arrays-are-equal-with-javascript
  if (a === b) return true
  if (!(a instanceof Array)) return false
  if (!(b instanceof Array)) return false
  if (a.length !== b.length) return false
  for (let i = 0; i !== a.length; ++i) {
    if (a[i] !== b[i]) return false
  }
  return true
}
// TODO: use native Array.find, Array.findIndex when IE support is dropped
export const arrayFindIndex = function(arr, pred) {
  for (let i = 0; i !== arr.length; ++i) {
    if (pred(arr[i])) {
      return i
    }
  }
  return -1
}

export const arrayFind = function(arr, pred) {
  const idx = arrayFindIndex(arr, pred)
  return idx !== -1 ? arr[idx] : undefined
}

// coerce truthy value to array
export const coerceTruthyValueToArray = function(val) {
  if (Array.isArray(val)) {
    return val
  } else if (val) {
    return [val]
  } else {
    return []
  }
}

export const isIE = function() {
  return !Vue.prototype.$isServer && !isNaN(Number(document.documentMode))
}

export const isEdge = function() {
  return !Vue.prototype.$isServer && navigator.userAgent.indexOf('Edge') > -1
}

export const isFirefox = function() {
  return !Vue.prototype.$isServer && !!window.navigator.userAgent.match(/firefox/i)
}

export const assert = (condition, msg) => {
  if (!condition) throw new Error(`[mds-ui] ${msg}`)
}
/**
 *
 * 特殊字符转译
 * @param {string} [value='']
 */
export const escapeRegexpString = (value = '') => String(value).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
/**
 * Get element offset width
 * @param {*} node HTMLDOMElemment
 * @return {Number} width
 */
/**
 * Get element offset width
 * @param {*} node HTMLDOMElemment
 * @return {Number} width
 */
export const getOffsetWidth = (node) => node ? node.offsetWidth : 0
